package com.sandbox.ws.client;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.namespace.QName;

import com.edb.finance.collateral.ws.checkin.v1.COLSCheckInServiceV1;
import com.edb.finance.collateral.ws.checkin.v1.COLSCheckInServiceV1PortType;
import com.edb.finance.collateral.ws.checkin.v1.CheckInRequest;
import com.edb.finance.collateral.ws.checkin.v1.CheckInResponse;
import com.edb.finance.collateral.wsdomain.collateralitem.v1.Collateralitem;
import com.edb.ws.wscommon_v21.EDBHeaderType;

/**
 * Uses the generated stub from the webservice-api project. Built with help from the apache cfx tutorial example at: http://cxf.apache.org/docs/developing-a-consumer.html
 * @author eu2445
 *
 */
public class CheckInClientImlp implements
		CheckInClient {

	private static int numberOfCalls=1;
	
	public void insertCollateralItems(List<Collateralitem> collateralItems) {

		final QName SERVICE_NAME = new QName(
				"urn:collateral.finance.edb.com:ws:checkin:v1", "COLSCheckInService_V1");
		
//		
//		File wsdlFile = new File("http://localhost:8080/soapuifiles/mockCOLSCheckInService_V1PortTypeSoap11?WSDL");
//		File wsdlFile = new File("http://se-r9hlt3n:8088/mockCOLSCheckInService_V1PortTypeSoap11?WSDL");
//	    File wsdlFile = new File("file:/C:/COLLATERAL/collateral/webservice/apis/target/generated-resources/wsfabric/wsdl/COLSCheckInService_V1.wsdl");
	    URL wsdlURL=null;
		try {
//			wsdlURL = new URL("http://localhost:8080/soap/mockCOLSCheckInService_V1PortTypeSoap11?WSDL"); //tomcat
			wsdlURL = new URL("http://se-r9hlt3n:8088/mockCOLSCheckInService_V1PortTypeSoap11?WSDL"); //soap ui
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		}
//	    COLSCheckInServiceV1 service = new COLSCheckInServiceV1(wsdlURL, SERVICE_NAME);
//	    COLSCheckInServiceV1PortType  port  = service.getCOLSCheckInServiceV1PortTypeSoap11();
//	    EDBHeaderType header = new EDBHeaderType();
	    
	    CheckInRequest checkInRequest = new CheckInRequest();
	    checkInRequest.getCollateralitem().addAll(collateralItems);
	    try{
//	    	printOutCollateralItemList(checkInRequest.getCollateralitem());
	    	System.out.println(numberOfCalls++ + "# Sending list["+ collateralItems.size() +"]" );
	    	System.out.println("CheckInClientImlp (Thread #" + Thread.currentThread() + ")");
	    	System.out.println("collateralItems.get(0).getCollateralItemType(): " + collateralItems.get(0).getCollateralItemType());
//	    	CheckInResponse checkInResponse = port.checkIn(header, checkInRequest);
//	    	System.out.println("checkInResponse.getResponseState().getMessage(): " + checkInResponse.getResponseState().getMessage());
//	    	System.out.println("checkInResponse.getResponseState().getErrorCode(): " + checkInResponse.getResponseState().getErrorCode());
	    	System.out.println("done cheking in collateralItems[" + collateralItems.get(0).getUniqueKey() +"]");
	    	Thread.sleep(300);
	    } catch(Exception e){
//	    	throw new InsertCollateralItemsException(e.getMessage());
	    }
	}

//	private void printOutCollateralItemList(List<Collateralitem> collateralItems) {
//		String time = new SimpleDateFormat("HH-mm-ss").format(new Date());
//	    System.out.print(numberOfCalls++ + "# ("+ time + "): Sending list["+ collateralItems.size() + "] containing: collateralItem[");
//	    for (Collateralitem collateralitem : collateralItems) {
//	    	System.out.print("{id: " + collateralitem.getUniqueKey() + ", type: " +  collateralitem.getCollateralItemType() + "}, ");
//		}
//	    System.out.print("]");
//	}
}
